package com.umeox.ota.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.lifecycle.ViewModelProviders;

import com.abupdate.trace.Trace;
import com.umeox.ota.OtaNewVersionFragmentBinding;
import com.umeox.ota.R;
import com.umeox.ota.mvvm.viewModel.NoticeViewModel;
import com.umeox.ota.ui.activity.MainActivity;
import com.umeox.ota.utils.ThemeColor;
import com.umeox.ota.utils.ViewUitls;


/**
 * Created by fighter_lee on 2017/8/23.
 */

public class OtaNewVersionFragment extends BaseFragment<OtaNewVersionFragmentBinding, NoticeViewModel> implements View.OnClickListener, View.OnFocusChangeListener {
    public static final String TAG = "OtaNewVersionFragment";

    @Override
    protected int genLayoutId(Bundle savedInstanceState) {
        return R.layout.fragment_new_version;
    }

    @Override
    protected void genViewModel() {
        mViewModel = ViewModelProviders.of(getActivity(), mViewModelFactory).get(NoticeViewModel.class);
    }

    private void download() {
        Intent intent = new Intent();
        intent.setClass(getActivity(), MainActivity.class);
        getActivity().startActivity(intent);
        getActivity().finish();
    }


    @Override
    public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mBinding.setViewmodel(mViewModel);
        mBinding.tvClickDownload.setOnClickListener(this);
        mBinding.tvClickCancel.setOnClickListener(this);
        mBinding.tvClickDownload.setOnFocusChangeListener(this);
        mBinding.tvClickCancel.setOnFocusChangeListener(this);
        mBinding.tvClickCancel.setNextFocusRightId(mBinding.tvClickDownload.getId());
        mBinding.tvClickDownload.setNextFocusLeftId(mBinding.tvClickCancel.getId());
        mBinding.tvClickDownload.requestFocus();
        return mBinding.getRoot();
    }

    @Override
    public void initData(Bundle savedInstanceState) {
    }

    @Override
    public void setData(Object data) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_click_download:
                download();
                Trace.d(TAG, "User click the download button");
                break;

            case R.id.tv_click_cancel:
                getActivity().finish();
                Trace.d(TAG, "User click the cancel button");
                break;
            default:
                break;
        }
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            v.setBackgroundColor(ViewUitls.darkenColor(ThemeColor.textColorPrimary, 0.6f));
        } else {
            v.setBackgroundColor(ThemeColor.textColorPrimary);
        }
    }
}
